package orxatas.travelme.activity;

import com.markupartist.android.widget.ActionBar;

import orxatas.travelme.R;
import android.app.TabActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainTabActivity extends TabActivity {
	private ActionBar actionBar;
	

	private boolean listenerRegistered = false;
	private SyncListener listener;
		
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_tabs);
        
        
  		actionBar = (ActionBar) findViewById(R.id.actionbar);
  		actionBar.setTitle(R.string.main_tab_title);
  		
  		listener = new SyncListener();

  		//Creamos una "acción"
  		actionBar.addAction(new ActionBar.Action() {
  			
  			/*
  			 * Acción que se realizará cuando se haga "click" en el icono de la acción.
  			 * */
  			@Override
  			public void performAction(View view) {
  				Intent action = new Intent();
  				action.setAction("SYNC_ACTION");
  				sendBroadcast(action);
  			}
  			
  			/*
  			 * Debe devolver el identificador del icono que se mostrará.
  			 * R.drawable.nuestroicono
  			 * */
  			@Override
  			public int getDrawable() {
  				return R.drawable.ic_menu_refresh;
  			}
  		});
  		
  		actionBar.addAction(new ActionBar.Action(){

			@Override
			public int getDrawable() {
				return R.drawable.ic_addfriend;
			}

			@Override
			public void performAction(View view) {
  				Intent action = new Intent();
  				action.setAction("SHOW_FRIENDS");
  				sendBroadcast(action);
			}
  			
  		});
         
        TabHost tabHost = getTabHost();       
        
        //First Tab
        Intent in = new Intent(this, GroupListActivity.class);
        TabSpec spec = tabHost.newTabSpec("TAB1")
        		.setIndicator(getString(R.string.main_tab_group_tab_title), getResources().getDrawable(R.drawable.ic_trips))
        		.setContent(in);
        
        tabHost.addTab(spec);
        
        //Second Tab
        in = new Intent(this, DiaryActivity.class);
        spec = tabHost.newTabSpec("TAB2")
        		.setIndicator(getString(R.string.main_tab_diary_tab_title), getResources().getDrawable(R.drawable.ic_diary))
        		.setContent(in);
        
        tabHost.addTab(spec);
        
        
        
        tabHost.setCurrentTabByTag("TAB1");
        
    }
    
	@Override
    protected void onResume() {
        super.onResume();

        if (!listenerRegistered) {
            registerReceiver(listener, new IntentFilter("SYNC_STARTED"));
            registerReceiver(listener, new IntentFilter("SYNC_ENDED"));
            listenerRegistered = true;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();

        if (listenerRegistered) {
            unregisterReceiver(listener);
            listenerRegistered = false;
        }
    }
    
    protected class SyncListener extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {

            // No need to check for the action unless the listener will
            // will handle more than one - let's do it anyway
            if (intent.getAction().equals("SYNC_STARTED")) {
            	actionBar.setProgressBarVisibility(View.VISIBLE);
            } else if(intent.getAction().equals("SYNC_ENDED")){
            	actionBar.setProgressBarVisibility(View.GONE);
            }
        }
    }
}
